﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Design;

namespace NHTrace
{
  /// <summary>
  /// This class implements the tool window exposed by this package and hosts a user control.
  ///
  /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
  /// usually implemented by the package implementer.
  ///
  /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
  /// implementation of the IVsWindowPane interface.
  /// </summary>
  [Guid("406553d3-1017-4516-af6b-0eff0e8154f9")]
  public class NHTraceToolWindow : ToolWindowPane
  {
    // This is the user control hosted by the tool window; it is exposed to the base class 
    // using the Window property. Note that, even if this class implements IDispose, we are
    // not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
    // the object returned by the Window property.
    private NHTraceToolWindowControl _control;

    /// <summary>
    /// Standard constructor for the tool window.
    /// </summary>
    public NHTraceToolWindow() :
      base(null)
    {
      ToolBar = new CommandID(
          GuidList.guidNHTraceCmdSet,
          PkgCmdIDList.NHTrace);
      ToolBarLocation = (int)VSTWT_LOCATION.VSTWT_LEFT;

      var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
      if (null != mcs)
      {
        var toolbarbtnCmdID = new CommandID(
            GuidList.guidNHTraceCmdSet,
            PkgCmdIDList.cmdidClearLog);
        var menuItem = new MenuCommand(new EventHandler(
            clearLogHandler), toolbarbtnCmdID);
        mcs.AddCommand(menuItem);

        toolbarbtnCmdID = new CommandID(
            GuidList.guidNHTraceCmdSet,
            PkgCmdIDList.cmdidToggleLogging);
        menuItem = new MenuCommand(new EventHandler(
            toggleLoggingHandler), toolbarbtnCmdID);
        mcs.AddCommand(menuItem);

        toolbarbtnCmdID = new CommandID(
            GuidList.guidNHTraceCmdSet,
            PkgCmdIDList.cmdidRefreshLog);
        menuItem = new MenuCommand(new EventHandler(
            refreshLogHandler), toolbarbtnCmdID);
        mcs.AddCommand(menuItem);

      } 



      // Set the window title reading it from the resources.
      Caption = Resources.ToolWindowTitle + " - Logging enabled"; 
      // Set the image that will appear on the tab of the window frame
      // when docked with an other window
      // The resource ID correspond to the one defined in the resx file
      // while the Index is the offset in the bitmap strip. Each image in
      // the strip being 16x16.
      BitmapResourceID = 301;
      BitmapIndex = 1;


      _control = new NHTraceToolWindowControl();
    }

    private void clearLogHandler(object sender, EventArgs arguments)
    {
      (Package as NHTracePackage).ClearMessages();
      (Package as NHTracePackage).WriteMessagesToWindow();
    }

    private void toggleLoggingHandler(object sender, EventArgs arguments)
    {
      (Package as NHTracePackage).LogEnabled = !(Package as NHTracePackage).LogEnabled;

      if ((Package as NHTracePackage).LogEnabled)
        Caption = Resources.ToolWindowTitle + " - Logging enabled";
      else
        Caption = Resources.ToolWindowTitle + " - Logging disabled";
    }

    private void refreshLogHandler(object sender, EventArgs arguments)
    {
      (Package as NHTracePackage).WriteMessagesToWindow();
    }

    /// <summary>
    /// This property returns the handle to the user control that should
    /// be hosted in the Tool Window.
    /// </summary>
    override public IWin32Window Window
    {
      get
      {
        return (IWin32Window)_control;
      }
    }

  }
}
